
from langchain_core.output_parsers import (
    CommaSeparatedListOutputParser,
    ListOutputParser,
    MarkdownListOutputParser,
    NumberedListOutputParser,
    PydanticOutputParser,
    XMLOutputParser,
)
from langchain_core.output_parsers.openai_tools import (
    JsonOutputKeyToolsParser,
    JsonOutputToolsParser,
    PydanticToolsParser,
)

from langchain._api import create_importer
from langchain.output_parsers.boolean import BooleanOutputParser
from langchain.output_parsers.combining import CombiningOutputParser
from langchain.output_parsers.datetime import DatetimeOutputParser
from langchain.output_parsers.enum import EnumOutputParser
from langchain.output_parsers.fix import OutputFixingParser
from langchain.output_parsers.pandas_dataframe import PandasDataFrameOutputParser
from langchain.output_parsers.regex import RegexParser
from langchain.output_parsers.regex_dict import RegexDictParser
from langchain.output_parsers.retry import RetryOutputParser, RetryWithErrorOutputParser
from langchain.output_parsers.structured import ResponseSchema, StructuredOutputParser
from langchain.output_parsers.yaml import YamlOutputParser
from pydantic import BaseModel,FilePath,Field
from langchain_core.utils.pydantic import (
    PydanticBaseModel,
    TBaseModel,
)

from langchain_core.beta.runnables.context import Context
from langchain_core.runnables.passthrough import RunnablePassthrough
from langchain_core.prompts.prompt import PromptTemplate
from langchain_core.output_parsers.string import StrOutputParser
from langchain.schema import HumanMessage, SystemMessage
 


from langchain.chat_models import ChatOpenAI
from langchain.output_parsers import PandasDataFrameOutputParser, OutputFixingParser
from langchain_core.prompts import PromptTemplate
import pandas as pd


llm = ChatOpenAI(
    model="deepseek-chat",
    temperature=0,
    openai_api_key="sk-605e60a1301040759a821b6b677556fb",
    base_url="https://api.deepseek.com/v1")

from langchain.output_parsers.structured import (
    StructuredOutputParser, ResponseSchema
)


'''

# 定义数据模型
class PersonInfo(BaseModel):
    name: str = Field(description="用户全名")
    age: int = Field(description="用户年龄", gt=0)
    skills: list[str] = Field(description="技能列表")
    is_employed: bool = Field(description="就业状态")

# 创建解析器和模型
parser = YamlOutputParser(pydantic_object=PersonInfo)
 

# 构建提示模板
prompt = PromptTemplate(
    template="根据用户输入生成个人信息。\n{format_instructions}\n输入: {query}\n",
    input_variables=["query"],
    partial_variables={"format_instructions": parser.get_format_instructions()}
)

# 创建处理链
chain = prompt | llm | parser

# 调用示例
result = chain.invoke({"query": "张三，28岁，会Python和数据分析，目前在职"})
print(result)

response_schemas = [
    ResponseSchema(
        name="foo",
        description="a list of strings",
        type="string"
        )
]

parser = StructuredOutputParser.from_response_schemas(response_schemas)

print(parser.get_format_instructions())

rs=parser.parse({"foo":"ss"})
print(rs)
 


df = pd.DataFrame(
    {
        "num_legs": [2, 4, 8, 0],
        "num_wings": [2, 0, 0, 0],
        "num_specimen_seen": [10, 2, 1, 8],
    }
)

parser = PandasDataFrameOutputParser(dataframe=df)


df_query = "检索 num_wings 列。"
prompt = PromptTemplate(
    template="回答用户查询。\n{format_instructions}\n{query}\n",
    input_variables=["query"],
    partial_variables={"format_instructions": parser.get_format_instructions()},
)

chain = prompt | llm | parser
parser_output = chain.invoke({"query": df_query})

print(parser_output)


outputParser=DatetimeOutputParser()
rs=outputParser.parse("2025-10-10 ")
print(rs)   




booloutputParser=XMLOutputParser()
  
outputParser=XMLOutputParser()

comoutputParser=CombiningOutputParser(name='ok' ,parsers=[booloutputParser,outputParser])
rs=comoutputParser.parse(<foo>\n   <bar>\n      <baz></baz>\n   </bar>\n</foo>)
print(rs)   



outputParser=BooleanOutputParser()
rs=outputParser.parse("yes")
print(rs)   




outputParser=XMLOutputParser()

rs=outputParser.parse(<foo>\n   <bar>\n      <baz></baz>\n   </bar>\n</foo>)
print(rs)   

 
outputParser=PydanticOutputParser(pydantic_object=Person)

rs=outputParser.parse({"name": "ni"})
print(rs)



outputParser=NumberedListOutputParser()

rs=outputParser.parse("\n\n1. foo\n\n2. bar\n\n3")
print(rs)





outputParser=MarkdownListOutputParser()

rs=outputParser.parse("- foo\n- bar\n- baz")
print(rs)

outputParser=CommaSeparatedListOutputParser()

rs=outputParser.parse("foo, bar, baz` or `foo,bar,baz")
print(rs)
'''